package com.suixinsanren.design.state.demo.demo04;

public class WinnerState implements State {

    private final GumballMachine gumballMachine;

    public WinnerState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {
        System.out.println("幸运星，我们正在为您准备糖果，请先不要投币");
    }

    @Override
    public void ejectQuarter() {
        System.out.println("幸运星，游戏已经开始，我们正在为您准备糖果，现在无法退还硬币");
    }

    @Override
    public void turnCrank() {
        System.out.println("幸运星，游戏还未结束，我们正在为您准备糖果");
    }

    @Override
    public void dispense() {
        if (gumballMachine.getCount() > 0) {
            gumballMachine.releaseBall();
            System.out.println("您获得了1颗糖果");
            if (gumballMachine.getCount() > 0) {
                gumballMachine.releaseBall();
                System.out.println("您又获得了1颗糖果，因为您是幸运星");
            }
            gumballMachine.setCurrentState(gumballMachine.getNoQuarterState());
        } else {
            System.out.println("抱歉，糖果已经售罄，您的硬币已退还");
            gumballMachine.setCurrentState(gumballMachine.getSoldOutState());
        }
    }

    @Override
    public String toString() {
        return "幸运星状态";
    }
}
